# -*- mode: shell-script; sh-basic-offset: 8; indent-tabs-mode: t -*-
# ex: ts=4 sw=4 et filetype=sh

# save or symlink this file to /etc/bash_completion.d/

#
# qis completion
#
_qis()
{
    local cur prev commands default_options options command

    COMPREPLY=()

    # Get current typing
    cur=`_get_cword`

    # Define list of available commands
    commands='all help init modules summary cs test coverage'
    default_options='--verbose -v --quiet -q --no-color'

    if [[ $COMP_CWORD -eq 1 ]] ; then
        if [[ "$cur" == -* ]]; then
            COMPREPLY=( $( compgen -W "--version --help -h --short $default_options" -- $cur ) )
        else
            COMPREPLY=( $( compgen -W "$commands" -- $cur ) )
        fi
    else
        prev=${COMP_WORDS[COMP_CWORD-1]}
        command=${COMP_WORDS[1]}

        # If the current word starts with a hyphen (option detection)
        if [[ "$cur" == -* ]]; then
            case $command in
                summary)
                    options='--short'
                    ;;
                cs)
                    options='--list --direct -d'
                    ;;
                coverage)
                    options='--list'
                    ;;
                test)
                    options='--list'
                    ;;
            esac

            # Add on the global (default) options
            options="$options $default_options"

            COMPREPLY=( $( compgen -W "$options" -- $cur ) )
        else
            if [[ "$command" == @(help|h|summary) ]]; then
                # The help or summary command should bash_complete
                # the list of available commands
                COMPREPLY=( $( compgen -W "$commands" -- $cur ) )
            else
                _filedir
            fi
        fi
    fi

    return 0
}
complete -F _qis qis
